package com.changgou.system.goods.controller;

import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Spu;
import com.changgou.system.goods.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/spu")
@RestController
public class SpuController {
    @Autowired
    private SpuService spuService;

    /***
     * 新增数据
     * @param goods
     * @return
     */
    @PostMapping
    public Result<Void> add(@RequestBody Goods goods) {
        spuService.add(goods);
        return new Result<>(true, StatusCode.OK, "添加成功");
    }

    @GetMapping("/{id}")
    public Result<Spu> findById(@PathVariable String id) {
        Goods goods = spuService.findGoodsById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", goods);
    }

    /***
     * 修改数据
     * @param goods
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<Void> update(@RequestBody Goods goods, @PathVariable String id) {
        spuService.update(goods);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    //TODO 3 商品审核与上下架
    /**
     * 审核
     * @param id
     * @return
     */
    @PutMapping("/audit/{id}")
    public Result<Void> audit(@PathVariable String id){
        spuService.audit(id);
        return new Result<>();
    }

    /**
     * 下架
     * @param id
     * @return
     */
    @PutMapping("/pull/{id}")
    public Result<Void> pull(@PathVariable String id){
        spuService.pull(id);
        return new Result<>();
    }

    /**
     * 上架
     * @param id
     * @return
     */
    @PutMapping("/put/{id}")
    public Result<Void> put(@PathVariable String id){
        spuService.put(id);
        return new Result<>();
    }
    @DeleteMapping("/delete/{id}")
    public Result<Void> delById(@PathVariable String id){
        spuService.delById(id);
        return new Result<>();
    }

    /**
     * 恢复数据
     * @param id
     * @return
     */
    @PutMapping("/restore/{id}")
    public Result<Void> restore(@PathVariable String id){
        spuService.restore(id);
        return new Result<>();
    }

    /**
     * 物理删除
     * @param id
     * @return
     */
    @DeleteMapping("/realDelete/{id}")
    public Result<Void> realDelete(@PathVariable String id){
        spuService.realDelete(id);
        return new Result<>();
    }
}